package com.juzi.wopu;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

class MyLogHander extends Formatter { 
    @Override 
    public String format(LogRecord record) { 
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return "["+record.getLevel() + "] "+ sdf.format(new Date()) +" :"+ record.getMessage()+"\r\n"; 
    } 
}

public class LoggerUtil {
	private static String file_name = "LogjavaApi";
	private static String filepath="";	
	
	private static String msgfilename="";	
	private static FileHandler msgfLog=null; 
	private static Logger logMsg;
	private static Lock locklog= null;
	
	private static String Errfilename="";	
	private static FileHandler ErrfLog=null; 
	private static Logger logErr;
	private static Lock lockErr= null;
	
	
	
	//加载的时候执行1次，只执行一次
	/*static
    {
		locklog= new ReentrantLock();
		if ((pb.m_RunPath.equals("")) || (pb.m_RunPath== null))
		{
			StringBuffer logPath = new StringBuffer();
	        logPath.append(System.getProperty("user.home"));
	        logPath.append(System.getProperty("file.separator") +file_name);
	        File file = new File(logPath.toString());
	        if (!file.exists())
	            file.mkdir();
	        filepath=logPath.toString();
		}
		else
		{
			File file = new File(pb.m_RunPath+System.getProperty("file.separator")+file_name);
	        if (!file.exists())
	            file.mkdir();
	        filepath=pb.m_RunPath+System.getProperty("file.separator")+file_name;
		}
        System.out.println(filepath);
        logu=Logger.getLogger("Loggg");
        
        log("系统初始化：",Level.SEVERE);
    }*/
	
	static
	{	
		String sLocalPath=pb.my_runpath()+System.getProperty("file.separator");
		File file = new File(sLocalPath+file_name);
	    if (!file.exists())
	    	file.mkdir();
	     filepath=sLocalPath+file_name;
		
        System.out.println(filepath);
        
        
        locklog= new ReentrantLock();
        logMsg=Logger.getLogger("Loggg");
        
        lockErr= new ReentrantLock();
        logErr=Logger.getLogger("LogErr");
        
        log("系统初始化：",Level.SEVERE);
        logErr("系统初始化：",Level.SEVERE);
    }
	
	public static void log(String slog,Level level)
	{
		locklog.lock();
		try
		{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			String slogpath=filepath+System.getProperty("file.separator") +"Log"+sdf.format(new Date())+".log";
			
			if ((!msgfilename.equals(slogpath)) &&(!msgfilename.equals(""))) 
			{
				msgfilename=slogpath;
				try {
					logMsg.removeHandler(msgfLog); //移除以前句柄
					msgfLog.close();
					
					msgfLog = new FileHandler(slogpath,true);
					logMsg.addHandler(msgfLog);//日志输出文件
					msgfLog.setFormatter(new MyLogHander());//输出格式          
					msgfLog.setEncoding("UTF-8");
		        } catch (SecurityException e) {
		        	logMsg.log(Level.SEVERE, "安全性错误", e);
		        } catch (IOException e) {
		        	logMsg.log(Level.SEVERE,"读取文件日志错误", e);
		        }
			}
			
	        if  (msgfilename.equals(""))
			{
				msgfilename=slogpath;
				try {
					msgfLog = new FileHandler(slogpath,true);
					logMsg.addHandler(msgfLog);//日志输出文件
		            //logger.setLevel(level);
		            //fLog.setFormatter(new SimpleFormatter());//输出格式
					msgfLog.setFormatter(new MyLogHander());//输出格式     
					msgfLog.setEncoding("UTF-8");
					//logMsg.addHandler(new ConsoleHandler());//输出到控制台
					
					
		        } catch (SecurityException e) {
		        	logMsg.log(Level.SEVERE, "安全性错误", e);
		        } catch (IOException e) {
		        	logMsg.log(Level.SEVERE,"读取文件日志错误", e);
		        }
				
			}
			
	        logMsg.log(level ,slog);
		}
		finally
		{
			locklog.unlock();
		}
		
	}
	
	
	public static void logErr(String slog,Level level)
	{
		lockErr.lock();
		try
		{
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			String slogpath=filepath+System.getProperty("file.separator") +"Err"+sdf.format(new Date())+".log";
			
			if ((!Errfilename.equals(slogpath)) &&(!Errfilename.equals(""))) 
			{
				Errfilename=slogpath;
				try {
					logErr.removeHandler(ErrfLog); //移除以前句柄
					ErrfLog.close();
					
					ErrfLog = new FileHandler(slogpath,true);
					logErr.addHandler(ErrfLog);//日志输出文件
					ErrfLog.setFormatter(new MyLogHander());//输出格式          
					ErrfLog.setEncoding("UTF-8");
		        } catch (SecurityException e) {
		        	logErr.log(Level.SEVERE, "安全性错误", e);
		        } catch (IOException e) {
		        	logErr.log(Level.SEVERE,"读取文件日志错误", e);
		        }
			}
			
	        if  (Errfilename.equals(""))
			{
	        	Errfilename=slogpath;
				try {
					ErrfLog = new FileHandler(slogpath,true);
					logErr.addHandler(ErrfLog);//日志输出文件
		            //logger.setLevel(level);
		            //fLog.setFormatter(new SimpleFormatter());//输出格式
					ErrfLog.setFormatter(new MyLogHander());//输出格式     
					ErrfLog.setEncoding("UTF-8");
		            //logger.addHandler(new ConsoleHandler());//输出到控制台
		        } catch (SecurityException e) {
		        	logErr.log(Level.SEVERE, "安全性错误", e);
		        } catch (IOException e) {
		        	logErr.log(Level.SEVERE,"读取文件日志错误", e);
		        }
				
			}
			
	        logErr.log(level ,slog);
		}
		finally
		{
			lockErr.unlock();
		}
		
	}
	
	public static void set(String slog)
	{
		log(slog,Level.SEVERE);
		logErr(slog,Level.SEVERE);
	}
	
	public static void Err(String slog)
	{
		log(slog,Level.WARNING);
		logErr(slog,Level.WARNING);
	}
	
	public static void Log(String slog)
	{
		log(slog,Level.INFO);
		
	}
	 
}
